package com.nstar.countdown.view.modules.impl
{
	import com.adobe.serialization.json.JSON;
	import com.jwopitz.containers.TileCanvas;
	import com.nstar.base.event.AdvanceEvent;
	import com.nstar.base.event.RequestEvent;
	import com.nstar.base.util.Logger;
	import com.nstar.base.util.ObjectUtils;
	import com.nstar.countdown.controller.CountDownManager;
	import com.nstar.countdown.model.vo.CountDownVO;
	import com.nstar.countdown.view.components.CountDownPanelSimple;
	import com.nstar.countdown.view.components.ToolTipPanel;
	import com.nstar.countdown.view.main.CountDownEditor;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Button;
	import mx.events.ChildExistenceChangedEvent;
	import mx.events.FlexEvent;
	import mx.messaging.messages.RemotingMessage;
	import mx.modules.Module;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class MineClass extends Module implements IResponder
	{
		public var btnAdd:Button;
		public var tcMine:TileCanvas;
		public var tcFavorate:TileCanvas;
		
		public var countDownManager:CountDownManager = new CountDownManager("Mine");
		
		private var logger:Logger = Logger.getInstance(this);
		
		public function MineClass()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			btnAdd.addEventListener(MouseEvent.CLICK,addCountDownHandler);
			tcFavorate.addEventListener(ChildExistenceChangedEvent.CHILD_ADD,favoriteStateHandler);
			tcFavorate.addEventListener(CountDownPanelSimple.REMOVE_FAVORITE,removeFavoriteHandler);
			tcMine.addEventListener(CountDownPanelSimple.REMOVE,removeHandler);
			tcMine.addEventListener(ChildExistenceChangedEvent.CHILD_ADD,myStateHandler);
			
			//查询Guest的数据
			findMyCountDown("0000000000");
			findFavorite("0000000000");
		}
		//查询我的节日
		private function findMyCountDown(userId:String):void{
			var cdmI:RemoteObject = countDownManager.findMyCountDown(userId);
			if(!cdmI.hasEventListener(ResultEvent.RESULT)){
				cdmI.addEventListener(ResultEvent.RESULT,mineResultHandler);
			}
			if(!cdmI.hasEventListener(FaultEvent.FAULT)){
				cdmI.addEventListener(FaultEvent.FAULT,faultHandler);
			}
		}
		//查询我的收藏
		private function findFavorite(userId:String):void{
			var cdmF:RemoteObject = countDownManager.findFavorite(userId);
			if(!cdmF.hasEventListener(ResultEvent.RESULT)){
				cdmF.addEventListener(ResultEvent.RESULT,favoriteResultHandler);
			}
			if(!cdmF.hasEventListener(FaultEvent.FAULT)){
				cdmF.addEventListener(FaultEvent.FAULT,faultHandler);
			}
		}
		
		//我的节日查询结果处理
		private function mineResultHandler(event:ResultEvent):void{
			var result:Array = JSON.decode(event.result as String);
			var ary:ArrayCollection = ObjectUtils.deserializeArray(result,
				ObjectUtils.packageHandlerCallback,"com.nstar.countdown.model.vo");
			tcMine.dataProvider = ary;
		}
		
		//我的收藏查询结果
		private function favoriteResultHandler(event:ResultEvent):void{
			var result:Array = JSON.decode(event.result as String);
			var ary:ArrayCollection = ObjectUtils.deserializeArray(result,
				ObjectUtils.packageHandlerCallback,"com.nstar.countdown.model.vo");
			tcFavorate.dataProvider = ary;
		}
		
		//出错处理
		private function faultHandler(event:FaultEvent):void{
			logger.debugMore("MineClass faultHandler",event.fault);
		}
		
		//切换节日状态
		private function favoriteStateHandler(event:ChildExistenceChangedEvent):void{
			Object(event.relatedObject).currentState = "favoriteView";
		}
		
		private function myStateHandler(event:ChildExistenceChangedEvent):void{
			Object(event.relatedObject).currentState = "myView";
		}		
		
		//删除我的收藏
		private function removeFavoriteHandler(event:AdvanceEvent):void{
			var ro:RemoteObject = countDownManager.deleteFavorite(event.request as String,"0000000000");
			ro.addEventListener(ResultEvent.RESULT,removeFavoriteResultHandler);
			ro.addEventListener(FaultEvent.FAULT,faultHandler);
		}
		
		//删除我的节日
		private function removeHandler(event:AdvanceEvent):void{
			var ro:RemoteObject = countDownManager.deleteCountDown(event.request as String,"0000000000");
			ro.addEventListener(ResultEvent.RESULT,removeResultHandler);
			ro.addEventListener(FaultEvent.FAULT,faultHandler);	
		}
		
		//添加我的节日事件监控
		private function addCountDownHandler(event:MouseEvent):void{
			var cde:CountDownEditor = CountDownEditor.show();
			cde.addEventListener(CountDownEditor.ADD_COUNT_DOWN,addHandler);
		}
		
		//添加我的节日
		private function addHandler(event:RequestEvent):void{
			var vo:CountDownVO = event.request as CountDownVO;
			//前台使用JSON对对象进行编码，在JAVA后台使用flexjson来进行反序列化
			var ro:RemoteObject = countDownManager.insertCountDown(JSON.encode(vo),"0000000000");
			ro.addEventListener(ResultEvent.RESULT,resultHandler);
			ro.addEventListener(FaultEvent.FAULT,faultHandler);			
		}
		
		private function resultHandler(event:ResultEvent):void{
			ToolTipPanel.show("添加我的节日 OK ！");
			//重新查询，刷新数据
			findMyCountDown("0000000000");
		}
		
		private function removeFavoriteResultHandler(event:ResultEvent):void{
			ToolTipPanel.show("删除收藏夹 OK ！");
			//取方法调用的参数，用以从列表中删除数据,body中放的是调用参数列表
			var ary:Object = RemotingMessage(event.token.message).body;
			//从列表中删除数据，并更新视图
			refreshList(ary,tcFavorate);
		}
		
		private function removeResultHandler(event:ResultEvent):void{
			ToolTipPanel.show("删除我的节日 OK ！");
			var ary:Object = RemotingMessage(event.token.message).body;
			refreshList(ary,tcMine);			
		}
		
		//刷新列表
		private function refreshList(body:Object,tc:TileCanvas):void{
			var countDownId:String = body[0] as String;			
			var idx:int = ObjectUtils.indexOf(tc.dataProvider,"countDownId",countDownId);
			if(idx!=-1){
				tc.dataProvider =  ObjectUtils.reArray(tc.dataProvider,idx);
			}else{ 
				logger.debug("idx=-1, this should not be happend!!");
			}
		}
		
		public function result(data:Object):void
		{
		}
		
		public function fault(info:Object):void
		{
		}
		
	}
}